package fragment;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.alibaba.fastjson.JSON;
import com.facebook.drawee.view.SimpleDraweeView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import Contants.Url;
import adapter.ShouYeGridAdapter;
import adapter.ShouYeViewPagerAdapter;
import bean.ShouYeGridInfo;
import bean.ShouYeInfo;
import bean.ShouYeViewPagerInfo;
import it.sephiroth.android.library.picasso.Picasso;
import utils.MyGridView;
import utils.OkHttpClientUtils;
import yztc.com.shiyuanbaoyou.R;

/**
 * Created by My on 2016/9/22.
 */
public class ShouYeFragment extends Fragment {
    private int page=1;
    private List<ShouYeViewPagerInfo.AdListBean> list = new ArrayList<>();
    private List<ShouYeInfo> list2 = new ArrayList<>();
    private List<ShouYeGridInfo.RowsBean> list3=new ArrayList<>();
    private List<ImageView> imageViewList = new ArrayList<>();
    private ImageView[] icons;
    private LinearLayout ll;
    private com.handmark.pulltorefresh.library.PullToRefreshScrollView sv;
    private ShouYeGridAdapter adapter2;
    private ImageView imageView;
    private SimpleDraweeView  iv1,iv2,iv3,iv4,iv5;
    private MyGridView gv;
    private ViewPager vp;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    list = (List<ShouYeViewPagerInfo.AdListBean>) msg.obj;
                    for (int i = 0; i < list.size(); i++) {
                        imageView = new ImageView(getContext());
                        Picasso.with(getContext()).load(list.get(i).getImgUrl()).into(imageView);
                        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                        imageViewList.add(imageView);
                    }
                    initIcons();
                    ShouYeViewPagerAdapter adapter = new ShouYeViewPagerAdapter(imageViewList);
                    vp.setAdapter(adapter);
                    break;
                case 2:
                    list2 = (List<ShouYeInfo>) msg.obj;
                    iv1.setImageURI(Uri.parse(list2.get(0).getImgUrl()));
                    iv2.setImageURI(Uri.parse(list2.get(1).getImgUrl()));
                    iv3.setImageURI(Uri.parse(list2.get(2).getImgUrl()));
                    iv4.setImageURI(Uri.parse(list2.get(3).getImgUrl()));
                    iv5.setImageURI(Uri.parse(list2.get(4).getImgUrl()));
                    break;
                case 3:
                    list3= (List<ShouYeGridInfo.RowsBean>) msg.obj;
                    adapter2=new ShouYeGridAdapter(list3,getContext());
                    gv.setAdapter(adapter2);
                    break;
                case 4:
                    adapter2.notifyDataSetChanged();
                    break;
            }
        }
    };

    private void initIcons() {
        icons=new ImageView[imageViewList.size()];
        if (imageViewList!=null && imageViewList.size()!=0){
            for (int i=0;i<imageViewList.size();i++){
                icons[i]=new ImageView(getContext());
                icons[i].setImageResource(R.mipmap.dot_white);
                icons[i].setLayoutParams(new ViewGroup.LayoutParams(ViewPager.LayoutParams.WRAP_CONTENT,ViewPager.LayoutParams.WRAP_CONTENT));
                icons[i].setMaxWidth(30);
                icons[i].setMaxHeight(30);
                icons[i].setAdjustViewBounds(true);
                icons[i].setPadding(15,0,0,0);
                ll.addView(icons[i]);

            }
            icons[0].setImageResource(R.mipmap.dot_black);
        }
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i=0;i<imageViewList.size();i++){
                    icons[i].setImageResource(R.mipmap.dot_white);
                }
                icons[position].setImageResource(R.mipmap.dot_black);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shouye, null);
        vp = (ViewPager) view.findViewById(R.id.vp_shouye);
        iv1 = (SimpleDraweeView) view.findViewById(R.id.iv_shouye1);
        iv2 = (SimpleDraweeView) view.findViewById(R.id.iv_shouye2);
        iv3 = (SimpleDraweeView) view.findViewById(R.id.iv_shouye3);
        iv4 = (SimpleDraweeView) view.findViewById(R.id.iv_shouye4);
        iv5 = (SimpleDraweeView) view.findViewById(R.id.iv_shouye5);
        gv= (MyGridView) view.findViewById(R.id.gv_shouye);
        sv= (com.handmark.pulltorefresh.library.PullToRefreshScrollView) view.findViewById(R.id.sv_shouye);
        ll= (LinearLayout) view.findViewById(R.id.ll_shouye_vp);
        initViewPager();
        initTuiJian();
        initGrid();
        init();
        return view;
    }

    private void init() {
        sv.setMode(PullToRefreshBase.Mode.BOTH);
        sv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                initGrid();
                sv.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                page++;
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String jsonString4=OkHttpClientUtils.getStringFromURL(getContext(),Url.URL_SHOUYE_GRID1+page+Url.URL_SHOUYE_GRID2,"");
                            if (!TextUtils.isEmpty(jsonString4)){}
                            ShouYeGridInfo data4=null;
                            data4=JSON.parseObject(jsonString4,ShouYeGridInfo.class);
                            List<ShouYeGridInfo.RowsBean> allList=data4.getRows();
                            list3.addAll(allList);
                            Message msg4=Message.obtain();
                            msg4.what=4;
                            msg4.obj=allList;
                            handler.sendMessage(msg4);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                sv.onRefreshComplete();
            }
        });

    }

    private void initGrid() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String jsonString3=OkHttpClientUtils.getStringFromURL(getContext(),Url.URL_SHOUYE_GRID1+1+Url.URL_SHOUYE_GRID2,"");
                    if (!TextUtils.isEmpty(jsonString3)){
                        ShouYeGridInfo data3=null;
                        data3=JSON.parseObject(jsonString3,ShouYeGridInfo.class);
                        list3=data3.getRows();
                        Message msg3=Message.obtain();
                        msg3.what=3;
                        msg3.obj=list3;
                        handler.sendMessage(msg3);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void initTuiJian() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String jsonString2 = OkHttpClientUtils.getStringFromURL(getContext(), Url.URL_SHOUYE_TUIJIAN, "");
                    Log.i("tag", "---------->" + jsonString2);
                    if (!TextUtils.isEmpty(jsonString2)) {
                        ShouYeInfo data2 = null;
                        list2 = JSON.parseArray(jsonString2, ShouYeInfo.class);
                        Log.i("tag", "----->" + list2);
                        Message msg2 = Message.obtain();
                        msg2.obj = list2;
                        msg2.what = 2;
                        handler.sendMessage(msg2);

                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

    private void initViewPager() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String jsonString1 = OkHttpClientUtils.getStringFromURL(getContext(), Url.URL_SHOUYE_VIEWPAGER, "");
                    if (!TextUtils.isEmpty(jsonString1)) {
                        ShouYeViewPagerInfo data1 = null;
                        data1 = JSON.parseObject(jsonString1, ShouYeViewPagerInfo.class);
                        list = data1.getAdList();
                        Message msg = Message.obtain();
                        msg.obj = list;
                        msg.what = 1;
                        handler.sendMessage(msg);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }

}
